<script setup lang="ts">
import { useUserStore } from '@/store/modules/user-store'
import { getLodop } from '@/lib/lodop/LodopFuncs'
import { ref } from 'vue'
import dayjs from 'dayjs'
import { checkStockApi } from '@/api/business/storage/stocktaking-api'
import printModal from '@/components/business/print-modal/index.vue'
import { useEnums } from '@/hooks/plugins/useEnums'
import { useTransformSpec } from '@/hooks/web/use-transform-spec'
const { getDescByValue } = useEnums()
const { transformSpec } = useTransformSpec()
const emit = defineEmits(['close', 'getList'])
const open = ref<boolean>(false)

const userStore = useUserStore()

const visible = ref(false)
// 详情
const options = ref([])
defineExpose({
  showDrawer
})
const goodsList = ref([])
const getGoodsList = async () => {
  loading.value = true
  const res = await checkStockApi.detail({ id: options.value?.id })
  goodsList.value = res.data.detail
  loading.value = false
}
function showDrawer(item) {
  options.value = item
  visible.value = true
  getGoodsList()
}
function onClose() {
  goodsList.value = []
  visible.value = false
}

const loading = ref(false)

const usePrint = () => {
  const onPrint = () => {
    const LODOP = getLodop()
    if (!LODOP) {
      open.value = true
      return
    }
    LODOP.PRINT_INITA('0mm', '0mm', '210mm', '297mm', '盘点单')
    LODOP.SET_PRINT_PAGESIZE(1, '210mm', '297mm', '盘点单')
    LODOP.ADD_PRINT_TABLE(0, '5%', '90%', '100%', document.querySelector('#printStocktaking')?.outerHTML)
    LODOP.SET_PRINT_STYLEA(0, 'TableRowThickNess', '12pt')
    LODOP.ADD_PRINT_TEXT(0, '5%', 196, 20, '县链云仓')
    LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1)
    LODOP.NewPageA()
    LODOP.PREVIEW()
    checkStockApi.detail({ id: options.value.id, is_print: true })
  }
  // 210mm×297mm
  return {
    onPrint
  }
}
const { onPrint } = usePrint()
const handleModalClose = () => {
  open.value = false
}
</script>

<template>
  <printModal :open="open" @cancel="handleModalClose" />
  <a-drawer title="盘点单打印" :open="visible" @close="onClose">
    <template #closeIcon>
      <close @on-close="onClose" />
    </template>
    <table id="printStocktaking" border="0" center cellSpacing="0" cellPadding="1" width="100%" style="border-collapse: collapse; font-size: 11pt">
      <thead>
        <tr>
          <td colspan="8">
            <div style="line-height: 30px; text-align: center; font-size: 16pt; font-weight: 700">{{ userStore.getCompanyInfo?.name }}盘点损益单</div>
            <font style="position: absolute; right: 0; top: 0">
              <span tdata="pageNO" format="#">#</span>
              <span>/</span>
              <span tdata="pageCount" format="#">##</span>
            </font>
          </td>
        </tr>
      </thead>
      <tbody v-loading="loading">
        <tr>
          <td colspan="8">盘点状态: {{ getDescByValue('CHECK_STATUS_ENUM', options.check_status) }}&nbsp;&nbsp;&nbsp;&nbsp;单据日期: {{ options?.create_time }}&nbsp;&nbsp;&nbsp;&nbsp;打印次数: {{ options?.printLog?.print_count || 1 }}&nbsp;&nbsp;&nbsp;&nbsp;打印时间: {{ dayjs().format('YYYY-MM-DD HH:mm:ss') }}&nbsp;&nbsp;&nbsp;&nbsp;盘点编号: {{ options?.no }}</td>
        </tr>
        <tr>
          <td colspan="8">盘盈数量: {{ options.check_status == 1 ? '' : options?.py_num }}&nbsp;&nbsp;&nbsp;&nbsp;盘盈金额: {{ options.check_status == 1 ? '' : Number(options?.py_money).toFixed(2) }}&nbsp;&nbsp;&nbsp;&nbsp;盘亏数量: {{ options.check_status == 1 ? '' : options?.pk_num }}&nbsp;&nbsp;&nbsp;&nbsp;盘亏金额: {{ options.check_status == 1 ? '' : Number(options?.pk_money).toFixed(2) }}</td>
        </tr>
        <tr>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>序号</b></div>
          </td>
          <td width="30%" style="border: 1pt solid #000">
            <div align="center"><b>商品名称</b></div>
          </td>
          <td width="15%" style="border: 1pt solid #000">
            <div align="center"><b>原库存</b></div>
          </td>
          <td width="15%" style="border: 1pt solid #000">
            <div align="center"><b>盘点后库存</b></div>
          </td>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>盈亏数量</b></div>
          </td>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>盈亏金额</b></div>
          </td>
          <td width="10%" style="border: 1pt solid #000">
            <div align="center"><b>备注</b></div>
          </td>
        </tr>
        <tr v-for="(item, index) in goodsList" :key="item.id">
          <td style="border: 1pt solid #000; text-align: center">{{ index + 1 }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ item?.goods?.goods_name }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ transformSpec(item.num, item?.goods?.spec) }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ options.check_status == 1 ? '' : transformSpec(item.check_num, item?.goods?.spec) }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ options.check_status == 1 ? '' : transformSpec(item.diff_num, item?.goods?.spec) }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ options.check_status == 1 ? '' : item.diff_price }}</td>
          <td style="border: 1pt solid #000; text-align: center">{{ item.remark }}</td>
        </tr>
        <tr>
          <td colspan="3">仓管签字：</td>
          <td colspan="2">盘点人签字：</td>
        </tr>
      </tbody>
    </table>
    <template #footer>
      <div class="flex justify-end">
        <a-button type="default" @click="onClose" style="margin-right: 8px">关闭</a-button>
        <a-button type="primary" @click="onPrint">打印</a-button>
      </div>
    </template>
  </a-drawer>
</template>
